package whu.o2o.widgets;

import whu.o2o.R;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TipDialog {
	private Context mContext;
	private AlertDialog mDialog = null;
	private LayoutInflater inflater = null;
	TextView ok = null; // ȷ��
	TextView cancle = null;// ȡ��
	TextView title = null;
	TextView content = null;
	RelativeLayout tipLayout = null;

	public TipDialog(Context ctx) {
		mContext = ctx;
		mDialog = new AlertDialog.Builder(mContext).create();
		Window window = getWindow();
		window.setWindowAnimations(R.style.mystyle); // ��Ӷ���
		inflater = LayoutInflater.from(mContext);
		tipLayout = (RelativeLayout) inflater.inflate(R.layout.barcode_dialog, null);
		ok = (TextView) tipLayout.findViewById(R.id.ok);
		cancle = (TextView) tipLayout.findViewById(R.id.cancle);
		title = (TextView) tipLayout.findViewById(R.id.title);
		content = (TextView) tipLayout.findViewById(R.id.content);
		mDialog.setCanceledOnTouchOutside(false);
	}

	public void setMessage(String msg) {
		content.setText(msg);
	}

	public void setMessage(int id) {
		content.setText(mContext.getString(id));
	}

	public void setTitle(int id) {
		title.setText(mContext.getString(id));
	}

	public void setTitle(String s) {
		title.setText(s);
	}

	public void show() {
		if (mDialog != null) {
			mDialog.show();
			mDialog.setContentView(tipLayout);
		}
	}

	public void diss() {
		if (mDialog != null) {
			mDialog.dismiss();
		}
	}

	public void setOkWidth(int w) {
		ok.setWidth(w);
	}

	public void setCancleWidth(int w) {
		cancle.setWidth(w);
	}

	public void setHiddenCancle() {
		cancle.setVisibility(View.GONE);
	}

	public void setPositiveButton(String text, OnClickListener listener) {
		ok.setVisibility(View.VISIBLE);
		ok.setText(text);
		if (listener == null) {
			ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mDialog != null) {
						mDialog.dismiss();
					}
				}
			});
		} else {
			ok.setOnClickListener(listener);
		}
	}

	public void setNegativeButton(String text, OnClickListener listener) {
		cancle.setVisibility(View.VISIBLE);
		cancle.setText(text);
		if (listener == null) {
			cancle.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (mDialog != null) {
						mDialog.dismiss();
					}
				}
			});
		} else {
			cancle.setOnClickListener(listener);
		}
	}

	public Window getWindow() {
		return mDialog.getWindow();
	}
}